package com.xblibo.xruth.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>Title: AutoWrite.java </p>
 * <p>Description:  </p>
 * <p>Copyright: 易宝 2011*版权所有 </p>
 * <p>Company: yeepay </p>
 * @author xblibo
 * @version 1.0 ,  2011  上午11:22:14
 */
public class AutoWrite {

	/**
	 * @param args
	 * @return void
	 * author  xblibo  2011 上午11:22:14
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		simple("Attendance");
	}
	
	public static void simple(String name)throws IOException{
		pwDao(name);
		pwDaoImpl(name);
		pwService(name);
		pwServiceImpl(name);
		pwController(name);
	}
	
	public static void batch()throws IOException{
		File file = new File("src/com/xblibo/xruth/entity");
		for(File f : file.listFiles()){
			String name=f.getName().substring(0,f.getName().indexOf("."));
			if(name!=null && !"".equals(name)){
				pwDao(name);
				pwDaoImpl(name);
				pwService(name);
				pwServiceImpl(name);
				pwController(name);
			}
		}
	}
	/**   
	 * @Title: EntityDaoImpl.java
	 * @Package com.tenement.dao.impl
	 * @Description: TODO(添加描述)
	 * @author libo
	 * @date 2010-7-1 下午10:12:25    
	 * @version V1.0   
	 */
	public static void pwDesc(PrintWriter pw,String className,String packageName){
		Date date = new Date();
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		pw.println("/**");
		pw.println(" * @Title: "+className+".java");
		pw.println(" * @Package "+packageName);
		pw.println(" * @Description TODO(添加描述)");
		pw.println(" * @author xblibo");
		pw.println(" * @date "+simple.format(date));
		pw.println(" * @version V1.0");
		pw.println(" */");
		
	}

	/**
	 * 写dao
	 */
	public static  void pwDao(File dao,String name)throws IOException{
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(dao)),true);
		pw.println("package com.xblibo.xruth.dao;");
		pw.println("");
		pw.println("import com.xblibo.xruth.common.BaseDao;");
		pw.println("import com.xblibo.xruth.entity."+name+";");
		pw.println("");
		pw.println("");
		pwDesc(pw,name+"Dao","com.xblibo.xruth.dao");
		pw.println("");
		pw.println("public interface "+name+"Dao extends BaseDao<"+name+",Integer>{");
		pw.println("");
		pw.println("}");
		pw.flush();
		pw.close();
	}

	/**
	 * 写daoImpl
	 */
	public static void pwDaoImpl(File daoImpl,String name)throws IOException{
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(daoImpl)),true);
		pw.println("package com.xblibo.xruth.dao.impl;");
		pw.println("");
		pw.println("import org.springframework.stereotype.Repository;");
		pw.println("import com.xblibo.xruth.common.BaseDaoImpl;");
		pw.println("import com.xblibo.xruth.dao."+name+"Dao;");
		pw.println("import com.xblibo.xruth.entity."+name+";");
		pw.println("");
		pw.println("");
		pwDesc(pw,name+"DaoImpl","com.xblibo.xruth.dao.impl");
		pw.println("");
		pw.println("@SuppressWarnings(\"unchecked\")");
		pw.println("@Repository(\""+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"Dao\")");
		pw.println("public class "+name+"DaoImpl extends BaseDaoImpl<"+name+",Integer> implements "+name+"Dao{");
		pw.println("");
		pw.println("}");
		pw.flush();
		pw.close();
	}

	/**
	 * 写service
	 */
	public static void pwService(File service,String name)throws IOException{
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(service)),true);
		pw.println("package com.xblibo.xruth.service;");
		pw.println("");
		pw.println("import com.xblibo.xruth.common.BaseService;");
		pw.println("import com.xblibo.xruth.entity."+name+";");
		pw.println("");
		pw.println("");
		pwDesc(pw,name+"Service","com.xblibo.xruth.service");
		pw.println("");
		pw.println("public interface "+name+"Service extends BaseService<"+name+",Integer>{");
		pw.println("");
		pw.println("}");
		pw.flush();
		pw.close();
	}

	/**
	 * 写serviceImpl
	 */
	public static void pwServiceImpl(File serviceImpl,String name)throws IOException{
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(serviceImpl)),true);
		pw.println("package com.xblibo.xruth.service.impl;");
		pw.println("");
		pw.println("import javax.annotation.Resource;");
		pw.println("import org.springframework.stereotype.Service;");
		pw.println("import com.xblibo.xruth.common.BaseServiceImpl;");
		pw.println("import com.xblibo.xruth.dao."+name+"Dao;");
		pw.println("import com.xblibo.xruth.service."+name+"Service;");
		pw.println("import com.xblibo.xruth.entity."+name+";");
		pw.println("");
		pw.println("");
		pwDesc(pw,name+"ServiceImpl","com.xblibo.xruth.service.impl");
		pw.println("");
		pw.println("@Service(\""+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"Service\")");
		pw.println("public class "+name+"ServiceImpl extends BaseServiceImpl<"+name+",Integer> implements "+name+"Service{");
		pw.println("");
		pw.println("	@Resource(name=\""+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"Dao\")");
		pw.println("	public void setBaseDao("+name+"Dao baseDao){");
		pw.println("		super.baseDao=baseDao;");
		pw.println("	}");
		pw.println("}");
		pw.flush();
		pw.close();
	}
	
	
	/**
	 * 写controller
	 */
	public static void pwController(File controller,String name)throws IOException{
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(controller)),true);
		pw.println("package com.xblibo.xruth.controller;");
		pw.println("");
		pw.println("import javax.annotation.Resource;");
		pw.println("import org.springframework.stereotype.Controller;");
		pw.println("import com.xblibo.xruth.common.BaseController;");
		pw.println("import com.xblibo.xruth.service."+name+"Service;");
		pw.println("import org.springframework.web.bind.annotation.RequestMapping;");
		//pw.println("import com.xblibo.xruth.entity."+name+";");
		pw.println("");
		pw.println("");
		pwDesc(pw,name+"Controller","com.xblibo.xruth.controller");
		pw.println("");
		pw.println("@Controller");
		pw.println("public class "+name+"Controller extends BaseController{");
		pw.println("");
		pw.println("	@Resource(name=\""+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"Service\")");
		pw.println("	private "+name+"Service "+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"Service;");
		pw.println("");
		pw.println("	@RequestMapping(value = \"/"+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"/list\")");
		pw.println("	public String list(){");
		pw.println("		");
		pw.println("		return \"/"+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"/"+name.toLowerCase()+"_list\";");
		pw.println("	}");
		pw.println("");
		
		pw.println("	@RequestMapping(value = \"/"+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"/add\")");
		pw.println("	public String add(){");
		pw.println("		");
		pw.println("		return \"/"+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"/"+name.toLowerCase()+"_add\";");
		pw.println("	}");
		pw.println("");
		
		pw.println("	@RequestMapping(value = \"/"+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"/modify\")");
		pw.println("	public String modify(){");
		pw.println("		");
		pw.println("		return \"/"+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"/"+name.toLowerCase()+"_modify\";");
		pw.println("	}");
		pw.println("");
		
		
		pw.println("	@RequestMapping(value = \"/"+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"/detail\")");
		pw.println("	public String detail(){");
		pw.println("		");
		pw.println("		return \"/"+name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toLowerCase())+"/"+name.toLowerCase()+"_detail\";");
		pw.println("	}");
		pw.println("");
		
		
		pw.println("}");
		pw.flush();
		pw.close();
	}
	
	public static void pwDao(String name)throws IOException{
		File controller = new File("src/com/xblibo/xruth/dao/"+name+"Dao.java");
		pwDao(controller, name);
	}
	public static void pwDaoImpl(String name)throws IOException{
		File controller = new File("src/com/xblibo/xruth/dao/impl/"+name+"DaoImpl.java");
		pwDaoImpl(controller, name);
	}
	public static void pwService(String name)throws IOException{
		File controller = new File("src/com/xblibo/xruth/service/"+name+"Service.java");
		pwService(controller, name);
	}
	public static void pwServiceImpl(String name)throws IOException{
		File controller = new File("src/com/xblibo/xruth/service/impl/"+name+"ServiceImpl.java");
		pwServiceImpl(controller, name);
	}
	public static void pwController(String name)throws IOException{
		File controller = new File("src/com/xblibo/xruth/controller/"+name+"Controller.java");
		pwController(controller, name);
	}
}
